package BlackJack.View
{
	import BlackJack.Model.VO.Player;
	import fl.controls.Button;
	import fl.controls.ComboBox;
	import fl.controls.TextArea;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;

	public class Debug extends Sprite
	{
		private var main:BlackJackMain;
		private var _display:MovieClip;
		/**	Button */
		private var btnDict:Dictionary      = new Dictionary();
		public static const Connect:String  = 'Connect';
		public static const CRC:String      = 'CRC';
		public static const Press:String    = 'Press';
		public static const Sample:String   = 'Sample';
		public static const Send:String     = 'Send';
		public static const Test1:String    = 'Test1';
		public static const Test2:String    = 'Test2';
		public static const Test3:String    = 'Test3';
		public static const Test4:String    = 'Test4';
		public static const Buttons:Array   = [Connect, CRC, Press, Sample, Send, Test1, Test2, Test3, Test4];
		/**	Textield and TextArea */
		private var txtDict:Dictionary      = new Dictionary();
		public static const Content:String  = 'Content';
		public static const Dump:String     = 'Dump';
		public static const Error:String    = 'Error';
		public static const Log:String      = 'Log';
		public static const Message:String  = 'Message';
		public static const CountIn:String  = 'CountIn';
		public static const CountOut:String = 'CountOut';
		public static const Texts:Array     = [Content, Dump, Error, Log, Message, CountIn, CountOut];
		/**	ComboBox */
		private var cmbDict:Dictionary      = new Dictionary();
		public static const IP:String       = 'IP';
		public static const Port:String     = 'Port';
		public static const Type:String     = 'Type';
		public static const ComboBoxs:Array = [IP, Port, Type];
		/**	Status Light */
		public static const Light:String    = 'Light';
		/**	Alias */
		private var aliasDict:Dictionary    = new Dictionary();

		public function Debug(root:BlackJackMain)
		{
			main = root;
		}

		public function get display():MovieClip
		{
			return _display;
		}

		public function set display(value:MovieClip):void
		{
			_display = value;
			addChild(value);
			var s:String;
			var btn:Button;
			var cmb:ComboBox;
			var obj:Object;
			var mc:MovieClip;
			for each (s in Buttons)
			{
				btn = value['btn' + s];
				btnDict[s] = btn;
				aliasDict[btn] = s;
				btn.focusEnabled = false;
				btn.addEventListener(MouseEvent.CLICK, btnHandler);
			}
			for each (s in Texts)
			{
				obj = value['txt' + s];
				txtDict[s] = obj;
				aliasDict[obj] = s;
			}
			for each (s in ComboBoxs)
			{
				cmb = value['cmb' + s];
				cmbDict[s] = cmb;
				aliasDict[cmb] = s;
				cmb.focusEnabled = false;
				cmb.addEventListener(MouseEvent.CLICK, btnHandler);
			}
			mc = value['o' + Light];
			mc.buttonMode = true;
			aliasDict[mc] = Light;
			mc.addEventListener(MouseEvent.CLICK, btnHandler);
			TxtArea(Error).text = 'TS000129';
		}

		private function btnHandler(event:MouseEvent):void
		{
			var s:String = aliasDict[event.currentTarget];
			LOG('btnHandler' + '\t' + s);
			switch (s)
			{
				case Connect:
				{
					//socket.connect('10.10.50.50', 20010);
					//socket.connect('10.10.50.39', 20012);
					break;
				}
				case CRC:
					break;
				case Press:
					break;
				case Sample:
					break;
				case Send:
					break;
				case Test1:
				{
					//client.ID = 'TS000107';
					//socket.connect('10.10.50.39', 20012);
					//visible = false;
					break;
				}
				case Test2:
				{
					//client.ID = 'TS000129';
					//socket.connect('10.10.50.39', 20012);
					//visible = false;
					break;
				}
				case Test3:
				{
					//client.ID = 'TS000114';
					//socket.connect('10.10.50.50', 20010);
					//visible = false;
					break;
				}
				case Test4:
				{
					//client.ID = 'TS000112';
					client.ID = TxtArea(Error).text.toUpperCase();
					socket.connect('10.10.50.50', 20010);
					//socket.connect('10.10.50.39', 20012);
					visible = false;
					break;
				}
				case Light:
				{
					visible = false;
					break;
				}
			}
		}

		public function LOG(value:String):void
		{
			//TxtArea(Log).appendText(value + '\n');
			TxtArea(Content).appendText(value + '\n');
			TxtArea(Content).verticalScrollPosition = TxtArea(Content).maxVerticalScrollPosition;
		}

		public function MESSAGE(value:String):void
		{
			TxtArea(Content).appendText(value + '\n');
			TxtArea(Content).verticalScrollPosition = TxtArea(Content).maxVerticalScrollPosition;
		}

		private function CMB(value:String):ComboBox
		{
			return cmbDict[value];
		}

		private function TxtArea(value:String):TextArea
		{
			return txtDict[value];
		}

		private function TxtField(value:String):TextField
		{
			return txtDict[value];
		}

		private function get socket():FGS12Processer
		{
			return main.socket;
		}

		private function get client():Player
		{
			return main.client;
		}
	}
}
